package com.zzjson.dbutils.dao;

import com.zzjson.dbutils.HikariUtil;
import com.zzjson.dbutils.dto.BlogDto;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.dbutils.dao</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年07月09日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class BlogDao {
	private static QueryRunner queryRunner;

	static {
		queryRunner = HikariUtil.getQueryRunner();
	}

	/**
	 * 返回单个对象，通过new BeanHandler<>(Class<?> clazz)来设置封装
	 *
	 * @param bid
	 * @throws SQLException
	 */
	public static void selectBlog(Integer bid) throws SQLException {
		String sql = "select * from blog where bid = ? ";
		Object[] params = new Object[]{bid};
		BlogDto blogDto = queryRunner.query(sql, new BeanHandler<>(BlogDto.class), params);
		System.out.println(blogDto);
	}

	/**
	 * 返回列表，通过new BeanListHandler<>(Class<?> clazz)来设置List的泛型
	 */
	public static void selectList() throws SQLException {
		String sql = "select * from blog";
		List<BlogDto> list = queryRunner.query(sql, new BeanListHandler<>(BlogDto.class));
		//list.forEach(System.out::println);
	}
}